package org.monkey.common.generator.formatter.entity;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import org.monkey.common.constant.util.MonkeyIoUtil;
import org.monkey.common.constant.util.MonkeyStrUtil;
import org.monkey.common.generator.entity.GeneratorConfig;
import org.monkey.common.generator.entity.JavaEntityInfo;
import org.monkey.common.generator.service.TemplateFileFormatter;
import org.monkey.common.generator.util.FormatUtil;
import org.springframework.stereotype.Component;

@Component
public class DtoTemplateFileFormatter implements TemplateFileFormatter {

  private String templateStr = null;

  @Override
  public String getTemplateStr() {
    if(StrUtil.isBlank(templateStr)){
      templateStr = loadTemplateStr("dto.ftl");
    }
    return templateStr;
  }

  @Override
  public String getTemplateName() {
    return "dto";
  }

  @Override
  public void format(File rootDir, List<JavaEntityInfo> javaEntityInfos, GeneratorConfig config) {
    String dtoDir = rootDir.getAbsolutePath() + "/" + config.getModuleName()+"/src/main/java/"+config.getPackageName().replace(".", "/")+"/entity/dto";
    MonkeyIoUtil.batchCreateDirs(dtoDir);
    for(JavaEntityInfo entity:javaEntityInfos){
      String dtoFile = dtoDir + "/"+ MonkeyStrUtil.upperFirstCase(entity.getEntityName()) +"DTO.java";
      FormatUtil.format(getTemplateStr(), dtoFile, config.toParamMap(entity));
    }
  }

  public static void main(String[] args) {
    File file = new File("D:\\workspace\\monkey-gospel\\monkey-common\\monkey-generator\\src\\main\\java\\org\\monkey\\common\\generator\\formatter\\template");
    File[] files = file.listFiles();
    List<String> ss = new ArrayList<>();
    for(File f:files){
      ss.add(f.getName().substring(0,f.getName().lastIndexOf(".")));
    }
    System.out.println(JSONUtil.toJsonStr(ss));
  }
}
